$(function() {
	laydate.render({
	    elem: '#year',
	    trigger: 'click',
	    type: 'year',
	    done: function(value, date) {
	        $('#year').val(value);
	    }
	})

	laydate.render({
	    elem: '#submit_year',
	    trigger: 'click',
	    type: 'year',
	    done: function(value, date) {
	        $('#submit_year').val(value);
	    }
	})

  $('#name').searchableSelect();

	$('#search').click(function(event) {
		search();
	});

	$("#reset").click(function(event) {
		$("#dept").val('-1');
		$("#year").val('');
		$("#submit_year").val('');
		$("#type").val('-1');
		$("#information").hide();
		$("#result").html('');
		$(".tu").hide();
	});

	$("#dept").change(function(event) {
		search();
	});
	$("#type").change(function(event) {
		search();
	});


});

function search(){
	var dept=$("#dept").val();
	var year=$("#year").val();
	var submit_year=$("#submit_year").val();
	var type=$("#type").val();
	var search_type=$('input[name="search_type"]:checked').val();
	// if(dept=='-1'){
	// 	window.parent.topMessagePrompt('error','请选择部门');
	// 	$("#result").html('');	
	// 	return;
	// }
	$.post(getRootPath()+"/Admin/Results/deptData", 
		{
			dept:dept,
			year:year,
			submit_year:submit_year,
			type:type,
			search_type:search_type
		}, 
		function(data) {
		if(data.status){
			var t=JSON.parse(data.v);
			//console.log(t)
			if(search_type==1){
				// 部门
				$('#information').show();
				$('.tu').hide();
				$("#dept_name").html(t.dept);

				// 搜索结果
				$("#result").html('');
				$.each(t.data, function(k, v) {
					if(v.data.length!=0){
						html1='<div class="col-xs-12"> <div class="page-header"> <h3>'+v.table_name+'</h3> </div><table  class="table  table-bordered table-hover "><thead> <tr>';
						if(v.data.length!=0){
							$.each(v.table_col, function(k1, v1) {
								html1+="<th style=\"width:350px;\"><div>"+v1+"</div></th>";
							});
							html1+=" </tr> </thead><tbody>";
							$.each(v.data, function(k1, v1) {
								html1+="<tr>";
								$.each(v1, function(k2, v2) {
									 if(k2=='id'){
										html1+="<td class='center'> <label class='pos-rel'> <input type='checkbox' class='ace' name='check["+k+"][]' value='"+v2+"' checked> <span class='lbl'></span> </label> </td><td>"+(k1+1)+"</td>";
									 }else{
										if(k2 == 'only_code'){
											html1+="<td><div style=\"word-wrap: break-word;width:100px;\">"+v2+"</div></td>";
										}else{
											html1+="<td>"+v2+"</td>";
										}
									 }
								});
								html1+="<tr>";
							});
						}else{
							html1 +='';
						}
						
						html1+="</tbody> </table></div>";
						$("#result").append(html1);
					}
					
				});
			}else{
				$('.tu').show();
				$("#result").html('');
				var html=$(".test");
				$.each(html, function(k, v) {
					$(v).html('')
				});
				var i=-1;
				$.each(t, function(k, v) {
					if(v.data.length>0){
							var option1 = {
								title:{
						            text:t[k].table_name
						        },
							    xAxis: {
							        type: 'category',
							        data: t[k].title
							    },	
							    yAxis:{},			    
							    series: [{
							        data: t[k].data,
							        type: 'line'
							    }],
								tooltip : {
									trigger: 'axis',
									axisPointer: {
										type: 'cross',
										label: {
											backgroundColor: '#6a7985'
										}
									}
								}
							};
							i++;
							echarts.init($(".test")[i]).setOption(option1);
					}
						
				});
			}
			
		}
	},'json');
}

function exportData(){
	var data=$("form").serialize();
	if(dept=='-1'){
		window.parent.topMessagePrompt('error','请选择部门');
		return;
	}
	if(data.indexOf("check") == -1){
		window.parent.topMessagePrompt('error','没有选择导出的数据');
		return;
	}
	var load_alert=loadAlert('数据统计中...');
	$.ajax({
		url: '	Admin/Results/deptDataExport',
		type: 'POST',
		dataType: 'json',
		data: data,
		success:function(data) {
			if(data.status){
				closeAlert(load_alert);
				window.location.href=data.v;
			}
		}
	})
	
}